package domain.worm;

import sounds.Sounds;
import images.Images;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.appearances.Appearance;

public class JumpingState extends WormState {
	
	private double speed = 200;
	
	private double vx;
	private double vy;
	private double time;

	public JumpingState(Worm worm) {		
		super(worm);
		if(this.getWorm().getDir() == 1)
			this.getWorm().setAppearance(this.getJumpRightAnimation());
		else
			this.getWorm().setAppearance(this.getJumpLeftAnimation());

		this.vx = speed * Math.cos(Math.toRadians(80)) * this.getWorm().getDir();
		this.vy = - speed * Math.sin(Math.toRadians(80));
		this.time = 0;
		(Sounds.sounds.get("jump")).play();
	}

	private Appearance getJumpLeftAnimation() {
		return Images.getAnimation("jumpingLeft", 0.1);
	}

	private Appearance getJumpRightAnimation() {
		return Images.getAnimation("jumpingRight", 0.1);
	}

	@Override
	public void update(DeltaState deltaState) {

		double delta = deltaState.getDelta();
		this.time += delta;

		
		double xdif, ydif;
		xdif = this.vx * delta;
		ydif = (this.vy * delta + 2*time*time*0.5);
		
		if(!this.getWorm().getScene().getTerrain().isCollision(this.getWorm().getX()+xdif , this.getWorm().getY()+ydif , this.getWorm().getDiameter(), this.getWorm().getHeight())){
			this.getWorm().updateCoords(xdif, ydif);

		}else{
			this.getWorm().setState(new StateFalling(this.getWorm()));
		}

	}
	
	public void toFallingState(){
	}
}

